package com.holike.crm.fragment.report;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gallopmark.recycler.adapterhelper.CommonAdapter;
import com.gallopmark.recycler.widgetwrapper.WrapperRecyclerView;

import pony.xcode.chart.BarChartView;
import pony.xcode.chart.data.BarChartData;
import pony.xcode.tablayout.SlidingTabLayout;
import pony.xcode.tablayout.OnTabSelectListener;

import com.holike.crm.R;
import com.holike.crm.activity.report.DealerMultiPerformanceActivity;
import com.holike.crm.base.BaseActivity;
import com.holike.crm.base.BaseFragment;
import com.holike.crm.base.FragmentHelper;
import com.holike.crm.bean.BusinessReferenceMainBean;
import com.holike.crm.bean.DictionaryBean;
import com.holike.crm.bean.MultiItem;
import com.holike.crm.dialog.PlainTextTipsDialog;
import com.holike.crm.helper.CenterAlignImageSpan;
import com.holike.crm.helper.PickerHelper;
import com.holike.crm.helper.TextSpanHelper;
import com.holike.crm.util.DensityUtil;
import com.holike.crm.util.ParseUtils;

import java.util.ArrayList;
import java.util.List;

import pony.xcode.chart.LineChartView;
import pony.xcode.chart.PieChartView;
import pony.xcode.chart.data.LineChartData;
import pony.xcode.chart.data.PieChartData;

/**
 * Created by pony on 2019/11/4.
 * Version v3.0 app报表
 * 生意内参首页
 */
class BusinessReferenceMainHelper extends FragmentHelper {
    private Callback mCallback;
    private ImageView mBackTopImageView; //回到顶部按钮
    private List<MultipleItem> mDataList;
    private MultipleItemAdapter mAdapter;
    private View mHeaderView;   //头部布局
    private TextSpanHelper mTextHelper;
    private StyleSpan mStyleSpan;
    private ForegroundColorSpan mColorSpan;
    private AbsoluteSizeSpan mSizeSpan;
    private static final int FLAGS = SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE;
    private static final String CODE_2000 = "\u2000";
    private static final String CODE_3000 = "\u3000";
    private int[] mColorArray;
    private String mMonth;  //选择的月份
    private int mDatetimeIndex = -1;
    private boolean mIncludeMonthTab = false;  //是否添加了可选月份的tabLayout
    private String mShopCode; //选择的门店id
    private String mSelectShopName;
    private List<BusinessReferenceMainBean.ShopBean> mShopList;  //可选择的门店列表

    private BusinessReferenceMainBean mResultMainBean;
    private int mPerformanceTab = 0;  //当前选中的业绩类型tab
    private int mColorsTab = 0; //当前选中的花色类型tab
    private int mSeriesTab = 0; //当前选中的系列类型tab
    private int mSourceTab = 0;  //客户来源选择的tab

    BusinessReferenceMainHelper(BaseFragment<?, ?> fragment, Callback callback) {
        super(fragment);
        this.mCallback = callback;
        initTitle(fragment.getArguments());
        mBackTopImageView = obtainView(R.id.iv_back_top);
        initRender();
        mSelectShopName = mContext.getString(R.string.all_stores);
        initRecycler();
        doRequest();
    }

    private void initTitle(Bundle bundle) {
        String title = mContext.getString(R.string.title_business_reference);
        if (bundle != null) {
            title = bundle.getString("title", mContext.getString(R.string.title_business_reference));
        }
        mFragment.setTitle(title);
    }

    private void initRender() {
        mTextHelper = TextSpanHelper.from(mContext);
        mStyleSpan = new StyleSpan(Typeface.BOLD);
        mColorSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.textColor4));
        mSizeSpan = new AbsoluteSizeSpan(mContext.getResources().getDimensionPixelSize(R.dimen.textSize_16), false);
        mColorArray = new int[]{ContextCompat.getColor(mContext, R.color.ring_one),
                ContextCompat.getColor(mContext, R.color.ring_tow),
                ContextCompat.getColor(mContext, R.color.ring_three),
                ContextCompat.getColor(mContext, R.color.ring_four),
                ContextCompat.getColor(mContext, R.color.ring_five),
                ContextCompat.getColor(mContext, R.color.ring_six)
        };
    }

    private void initRecycler() {
        final WrapperRecyclerView wrapperRv = obtainView(R.id.rv_wrapper);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        wrapperRv.setLayoutManager(layoutManager);
        initHeader(wrapperRv);
        mDataList = new ArrayList<>();
        mAdapter = new MultipleItemAdapter(mContext, mDataList);
        wrapperRv.setAdapter(mAdapter);
        wrapperRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                //获取RecyclerView当前顶部显示的第一个条目对应的索引
                int position = layoutManager.findFirstVisibleItemPosition();
                //根据索引来获取对应的itemView
                View firstChildView = layoutManager.findViewByPosition(position);
                if (firstChildView != null) {
                    //获取当前显示条目的高度
                    int itemHeight = firstChildView.getHeight();
                    //获取当前RecyclerView 偏移量
                    int flag = (position) * itemHeight - firstChildView.getTop();
                    //注意事项：recyclerView不要设置padding
                    if (flag == 0)
                        mBackTopImageView.setVisibility(View.GONE);
                    else
                        mBackTopImageView.setVisibility(View.VISIBLE);
                }
            }
        });
        mBackTopImageView.setOnClickListener(view -> {
            mBackTopImageView.setVisibility(View.GONE);
            wrapperRv.scrollToPosition(0);
        });
    }

    //添加头部布局
    private void initHeader(WrapperRecyclerView wrapperRv) {
        mHeaderView = LayoutInflater.from(mContext).inflate(R.layout.header_business_reference, wrapperRv, false);
        wrapperRv.addHeaderView(mHeaderView);
    }

    private void doRequest() {
        mCallback.onRequest(mShopCode, mMonth);
    }

    /*加载失败*/
    void onFailure(String failReason) {
        setPageError(R.drawable.no_network, failReason, true);
    }

    private void setPageError(int drawableId, String text, boolean reload) {
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) mHeaderView.getLayoutParams();
        params.height = RecyclerView.LayoutParams.MATCH_PARENT;
        View view = LayoutInflater.from(mContext).inflate(R.layout.include_empty_page, refactor(), true);
        view.findViewById(R.id.ll_empty_page).setBackgroundResource(R.color.color_while);
        TextView tv = view.findViewById(R.id.tv_empty_page);
        tv.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(mContext, drawableId), null, null);
        tv.setText(text);
        TextView tvReload = view.findViewById(R.id.btn_empty_page_reload);
        if (reload) {
            tvReload.setVisibility(View.VISIBLE);
            tvReload.setOnClickListener(v -> reload());
        }
        mDataList.clear();
        mAdapter.notifyDataSetChanged();
    }

    private void noResult() {
        setPageError(R.drawable.no_result, mContext.getString(R.string.tips_noresult), false);
    }

    private FrameLayout refactor() {
        FrameLayout container = mHeaderView.findViewById(R.id.fl_header_container);
        if (container.getChildCount() > 0) {
            container.removeAllViews();
        }
        return container;
    }

    /*重新加载*/
    private void reload() {
        refactor();
        doRequest();
    }

    void onSuccess(BusinessReferenceMainBean bean) {
        if (bean == null) {
            noResult();
        } else {
            mResultMainBean = bean;
            updateHeader();
            dataUpdate();
        }
    }

    private void updateHeader() {
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) mHeaderView.getLayoutParams();
        params.height = RecyclerView.LayoutParams.WRAP_CONTENT;
        List<BusinessReferenceMainBean.SelectMonthBean> monthList = mResultMainBean.getMonList();
        if (!monthList.isEmpty() && !mIncludeMonthTab) {
            LinearLayout llContainer = mHeaderView.findViewById(R.id.ll_header_container);
            if (llContainer.getChildCount() > 1) {
                llContainer.removeViewAt(0);
            }
            View view = LayoutInflater.from(mContext).inflate(R.layout.include_common_slidingtablayout, llContainer, false);
            SlidingTabLayout tabLayout = view.findViewById(R.id.sliding_tab_layout);
            String[] titles = new String[monthList.size()];
            for (int i = 0; i < monthList.size(); i++) {
                titles[i] = monthList.get(i).name;
            }
            tabLayout.setupViewPager(view.findViewById(R.id.view_pager), titles);
            if (monthList.size() > 1) {
                tabLayout.setCurrentTab(1);
            }
            tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
                @Override
                public void onTabSelect(int position) {
                    if (position >= 0 && position < monthList.size()) {
                        mDatetimeIndex = position;
                        mMonth = monthList.get(position).selectTime;
                        doRequest();
                    }
                }

                @Override
                public void onTabReselect(int position) {

                }
            });
            llContainer.addView(view, 0);
            mIncludeMonthTab = true;
        }
        if (mShopList == null || !mResultMainBean.getShopList().isEmpty()) {
            mShopList = new ArrayList<>(mResultMainBean.getShopList());
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.header_business_reference_child, refactor(), true);
        TextView tvSelectShop = view.findViewById(R.id.tv_select_shop);
        tvSelectShop.setText(TextUtils.isEmpty(mSelectShopName) ? mContext.getString(R.string.all_stores) : mSelectShopName);
        tvSelectShop.setOnClickListener(v -> {
            List<DictionaryBean> optionItems = new ArrayList<>();
            for (BusinessReferenceMainBean.ShopBean shopBean : mShopList) {
                optionItems.add(new DictionaryBean(shopBean.shopId, shopBean.shopName));
            }
            PickerHelper.showOptionsPicker(mContext, optionItems, (position, b) -> {
                mShopCode = b.id;
                mSelectShopName = b.name;
                tvSelectShop.setText(b.name);
                doRequest();
            });
        });
        ((TextView) view.findViewById(R.id.tv_time_detail)).setText(mResultMainBean.timeData);  //显示时间段
    }

    private void dataUpdate() {
        List<MultipleItem> dataList = new ArrayList<>();
        if (mPerformanceTab == 1) {
            dataList.add(new MultipleItem(MultipleItem.TYPE_PERFORMANCE_RATIO_TITLE, mContext.getString(R.string.performance_ratio), mContext.getString(R.string.more_homedelivery_arrow)));
        } else {
            dataList.add(new MultipleItem(MultipleItem.TYPE_PERFORMANCE_RATIO_TITLE, mContext.getString(R.string.performance_ratio), ""));
        }
        dataList.add(new MultipleItem(MultipleItem.TYPE_PERFORMANCE_RATIO_TAB));
        if (mPerformanceTab == 3) {
            dataList.add(new PerformanceRatioAloneItem());
        } else {
            dataList.add(new PerformanceRatioItem(mPerformanceTab)); //业绩占比-content
        }
        dataList.add(new MultipleItem(MultipleItem.TYPE_COLOR_PERFORMANCE_TITLE, mContext.getString(R.string.top5_color_performance), mContext.getString(R.string.more_colors_arrow))); //花色业绩-title
        dataList.add(new ColorPerformanceItem(mColorsTab)); //花色业绩-content
        dataList.add(new MultipleItem(MultipleItem.TYPE_SERIES_PERFORMANCE_TITLE, mContext.getString(R.string.top5_series_performance), mContext.getString(R.string.more_series_arrow))); //系列业绩-title
        dataList.add(new SeriesPerformanceItem(mSeriesTab)); //系列业绩-content
        dataList.add(new MultipleItem(MultipleItem.TYPE_CUSTOMER_SOURCE_TITLE, mContext.getString(R.string.tips_customer_source), "")); //客户来源-title
        dataList.add(new CustomerSourceItem(mSourceTab));  //客户来源-content
        dataList.add(new MultipleItem(MultipleItem.TYPE_CUSTOMER_ANALYSIS_TITLE, mContext.getString(R.string.customer_analysis), mContext.getString(R.string.the_data_description)));  //客户分析-title
        dataList.add(new CustomerAnalysisItem(mResultMainBean.getCustomerDetail())); //客户分析-content
        dataList.add(new MultipleItem(MultipleItem.TYPE_CUSTOMER_SATISFACTION_TITLE, mContext.getString(R.string.customer_satisfaction_tips), "")); //客户满意度-title
        dataList.add(new CustomerSatisfactionItem(mResultMainBean.getCustomerSatisfactionMap())); //客户满意度-content
        dataList.add(new MultipleItem(MultipleItem.TYPE_SHOP_BENEFIT_TITLE, mContext.getString(R.string.shop_benefit), mContext.getString(R.string.the_data_description))); //门店效益-title
        dataList.add(new ShopBenefitItem(mResultMainBean.getShopDetail())); //门店效益-content
//        dataList.add(new MultipleItem(MultipleItem.TYPE_PROFITABILITY_TITLE, mContext.getString(R.string.profitability), mContext.getString(R.string.the_data_description))); //盈利状况-title
//        dataList.add(new ProfitabilityItem()); //盈利状况-content
        dataList.add(new MultipleItem(MultipleItem.TYPE_DEALER_RANKING_TITLE, mContext.getString(R.string.dealer_ranking), "")); //经销商排名-title
        dataList.add(new DealerRankingOneItem(mResultMainBean.getDealerRankingMap()));
        dataList.add(new DealerRankingSecondItem(mResultMainBean.getDealerRankingMap().getYearFinanceRank()));
        dataList.add(new DealerRankingThirdItem(mResultMainBean.getDealerRankingMap().getLevel()));
        dataList.add(new DealerRankingFourItem(mResultMainBean.getDealerRankingMap()));
        mDataList.clear();
        mDataList.addAll(dataList);
        mAdapter.notifyDataSetChanged();
    }

    class MultipleItem implements MultiItem {
        static final int TYPE_PERFORMANCE_RATIO_TITLE = 0; //业绩占比
        static final int TYPE_PERFORMANCE_RATIO_TAB = 1; //业绩占比分类tab
        static final int TYPE_PERFORMANCE_RATIO = 2;    //业绩占比（除客户类型item）
        static final int TYPE_PERFORMANCE_RATIO_ALONE = 3; //业绩占比（客户类型item）
        static final int TYPE_COLOR_PERFORMANCE_TITLE = 4; //花色业绩
        static final int TYPE_COLOR_PERFORMANCE = 5;
        static final int TYPE_SERIES_PERFORMANCE_TITLE = 6; //系列业绩
        static final int TYPE_SERIES_PERFORMANCE = 7;
        static final int TYPE_CUSTOMER_SOURCE_TITLE = 8; //客户来源
        static final int TYPE_CUSTOMER_SOURCE = 9;
        static final int TYPE_CUSTOMER_ANALYSIS_TITLE = 10; //客户分析
        static final int TYPE_CUSTOMER_ANALYSIS = 11;
        static final int TYPE_CUSTOMER_SATISFACTION_TITLE = 12; //客户满意度
        static final int TYPE_CUSTOMER_SATISFACTION = 13;
        static final int TYPE_SHOP_BENEFIT_TITLE = 14;  //门店效益
        static final int TYPE_SHOP_BENEFIT = 15;
        static final int TYPE_PROFITABILITY_TITLE = 16; //盈利状况
        static final int TYPE_PROFITABILITY = 17;
        static final int TYPE_DEALER_RANKING_TITLE = 18; //经销商排行
        static final int TYPE_DEALER_RANKING_ONE = 19;
        static final int TYPE_DEALER_RANKING_TWO = 20;
        static final int TYPE_DEALER_RANKING_THREE = 21;
        static final int TYPE_DEALER_RANKING_FOUR = 22;

        int itemType;
        CharSequence title;
        CharSequence menu;

        MultipleItem(int itemType) {
            this.itemType = itemType;
        }

        MultipleItem(int itemType, CharSequence title, CharSequence menu) {
            this(itemType);
            this.title = title;
            this.menu = menu;
        }

        @Override
        public int getItemType() {
            return itemType;
        }
    }

    class MultiPerformanceItem extends MultipleItem {
        CharSequence performance;
        CharSequence theFirst;
        CharSequence theSecond;
        CharSequence theThird;
        CharSequence theFourth;
        CharSequence theFifth;
        CharSequence theOther;

        MultiPerformanceItem(int itemType) {
            super(itemType);
        }

        SpannableString getTextSpan(String name, String performance, String percent) {
            String source = TextUtils.isEmpty(name) ? "" : name + CODE_3000;
            int start = source.length();
            source += TextUtils.isEmpty(performance) ? "" : performance + CODE_3000;
            source += TextUtils.isEmpty(percent) ? "" : percent;
            SpannableString ss = new SpannableString(source);
            ss.setSpan(mStyleSpan, start, source.length(), FLAGS);
            return ss;
        }
    }

    /*业绩占比*/
    class PerformanceRatioItem extends MultiPerformanceItem {
        CharSequence yearOnYear;  //同比增长
        CharSequence ringGrowth; //环比增长
        List<PieChartData> pieChartData;
        List<BarChartData> barChartData;

        PerformanceRatioItem(int index) {
            super(MultipleItem.TYPE_PERFORMANCE_RATIO);
            setTabIndex(index);
        }

        /*还原值*/
        void revert() {
            yearOnYear = null;
            ringGrowth = null;
            theFirst = null;
            theSecond = null;
            theThird = null;
            theFourth = null;
            theFifth = null;
            theOther = null;
            if (pieChartData != null) {
                pieChartData.clear();
                pieChartData = null;
            }
            if (barChartData != null) {
                barChartData.clear();
                barChartData = null;
            }
        }

        void setTabIndex(int index) {
            revert();
            pieChartData = new ArrayList<>();
            String total = "-";  //总业绩
            BusinessReferenceMainBean.MultipleMapBean mapBean;
            if (index == 0) {  //板材
                mapBean = mResultMainBean.getMaterialMap();
                List<BusinessReferenceMainBean.MaterialMapBean.DataBean> dataList = mResultMainBean.getMaterialMap().getData();
                if (!dataList.isEmpty() && dataList.get(0) != null) {
                    BusinessReferenceMainBean.MaterialMapBean.DataBean bean = dataList.get(0);
                    total = bean.getSumMoney();
                    String[] array = mContext.getResources().getStringArray(R.array.board_type);
                    theFirst = getTextSpan(array[0], bean.getOriginalBoard(), bean.getOriginalBoardRatio()); //原态板
                    theSecond = getTextSpan(array[1], bean.getOriginalKBroad(), bean.getOriginalKBroadRatio());  //原态K板
                    theThird = getTextSpan(array[2], bean.getGranuleBoard(), bean.getGranuleBoardRatio()); //颗粒板
                    theFourth = getTextSpan(array[3], bean.getHomeParticleBoard(), bean.getHomeParticleBoardRatio()); //家装颗粒板
                    pieChartData.add(new PieChartData(bean.getOriginalBoardValue(), mColorArray[0]));
                    pieChartData.add(new PieChartData(bean.getOriginalKBroadValue(), mColorArray[1]));
                    pieChartData.add(new PieChartData(bean.getGranuleBoardValue(), mColorArray[2]));
                    pieChartData.add(new PieChartData(bean.getHomeParticleBoardValue(), mColorArray[3]));
                }
            } else if (index == 1) {  //品类
                mapBean = mResultMainBean.getCategoryMap();
                List<BusinessReferenceMainBean.CategoryMapBean.DataBean> dataList = mResultMainBean.getCategoryMap().getData();
                if (!dataList.isEmpty() && dataList.get(0) != null) {
                    BusinessReferenceMainBean.CategoryMapBean.DataBean bean = dataList.get(0);
                    total = bean.getTotal();
                    String[] array = mContext.getResources().getStringArray(R.array.category_type);
                    theFirst = getTextSpan(array[0], bean.getCustomize(), bean.getCustomizeRatio()); //定制
                    theSecond = getTextSpan(array[1], bean.getCupboard(), bean.getCupboardRatio());  //橱柜
                    theThird = getTextSpan(array[2], bean.getWoodenDoor(), bean.getWoodenDoorRatio()); //木门
                    theFourth = getTextSpan(array[3], bean.getHouse(), bean.getHouseRatio()); //宅配
                    pieChartData.add(new PieChartData(bean.getCustomizeValue(), mColorArray[0]));
                    pieChartData.add(new PieChartData(bean.getCupboardValue(), mColorArray[1]));
                    pieChartData.add(new PieChartData(bean.getWoodenDoorValue(), mColorArray[2]));
                    pieChartData.add(new PieChartData(bean.getHouseValue(), mColorArray[3]));
                }
            } else if (index == 2) { //空间
                mapBean = mResultMainBean.getHouseMap();
                BusinessReferenceMainBean.HouseMapBean bean = mResultMainBean.getHouseMap();
                List<BusinessReferenceMainBean.HouseMapBean.DataBean> dataList = bean.getData();
                total = mResultMainBean.getHouseMap().getTotal();
                theFirst = getSpaceTextSpanByPosition(dataList, 0);
                theSecond = getSpaceTextSpanByPosition(dataList, 1);
                theThird = getSpaceTextSpanByPosition(dataList, 2);
                theFourth = getSpaceTextSpanByPosition(dataList, 3);
                theFifth = getSpaceTextSpanByPosition(dataList, 4);
                theOther = getSpaceTextSpanByPosition(dataList, 5);
                for (int i = 0; i < mColorArray.length; i++) {
                    if (i >= dataList.size()) {
                        break;
                    } else {
                        pieChartData.add(new PieChartData(dataList.get(i).getHouseMoneyValue(), mColorArray[i]));
                    }
                }
            } else { //门店
                mapBean = mResultMainBean.getShopTypeMap();
                BusinessReferenceMainBean.ShopTypeMapBean bean = mResultMainBean.getShopTypeMap();
                List<BusinessReferenceMainBean.ShopTypeMapBean.DataBean> dataList = bean.getData();
                total = mResultMainBean.getShopTypeMap().getTotal();
                theFirst = getShopTextSpanByPosition(dataList, 0);
                theSecond = getShopTextSpanByPosition(dataList, 1);
                theThird = getShopTextSpanByPosition(dataList, 2);
                theFourth = getShopTextSpanByPosition(dataList, 3);
                theFifth = getShopTextSpanByPosition(dataList, 4);
                theOther = getShopTextSpanByPosition(dataList, 5);
                for (int i = 0; i < mColorArray.length; i++) {
                    if (i >= dataList.size()) {
                        break;
                    } else {
                        pieChartData.add(new PieChartData(dataList.get(i).getMoneyValue(), mColorArray[i]));
                    }
                }
            }
            String lastYearPercent;
            double lastYearValue;
            String lastMonthPercent;
            double lastMonthValue;
            if (mapBean == null) {
                lastYearPercent = "";
                lastMonthPercent = "";
                lastYearValue = 0;
                lastMonthValue = 0;
            } else {
                lastYearPercent = mapBean.getLastYearPercentText();
                lastMonthPercent = mapBean.getLastMonthPercentText();
                lastYearValue = mapBean.getLastYearPercentValue();
                lastMonthValue = mapBean.getLastMonthPercentValue();
            }
            performance = mTextHelper.obtain((TextUtils.equals(mMonth, "0") || mDatetimeIndex == 0) ? R.string.annual_performance : R.string.this_month_performance, "\n" + total,
                    mColorSpan, mStyleSpan, new AbsoluteSizeSpan(mContext.getResources().getDimensionPixelSize(R.dimen.textSize_16), false));
            ForegroundColorSpan lastYearSpan;
            if (!TextUtils.isEmpty(lastYearPercent)) {
                if (lastYearValue > 0) {
                    lastYearSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.green));
                } else if (lastYearValue < 0) {
                    lastYearSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.bg_homepage_new));
                } else {
                    lastYearSpan = mColorSpan;
                }
            } else {
                lastYearSpan = mColorSpan;
            }
            yearOnYear = mTextHelper.obtain(R.string.growth_year_on_year, CODE_2000 + lastYearPercent, lastYearSpan, mStyleSpan);
            if (!TextUtils.isEmpty(lastMonthPercent)) {
                ForegroundColorSpan lastMonthSpan;
                if (lastMonthValue > 0) {
                    lastMonthSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.green));
                } else if (lastMonthValue < 0) {
                    lastMonthSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.bg_homepage_new));
                } else {
                    lastMonthSpan = mColorSpan;
                }
                ringGrowth = mTextHelper.obtain(R.string.growth_in_the_ring, CODE_2000 + lastMonthPercent, lastMonthSpan, mStyleSpan);
            } else {
                ringGrowth = null;
            }
        }

        private CharSequence getSpaceTextSpanByPosition(List<BusinessReferenceMainBean.HouseMapBean.DataBean> dataList, int position) {
            if (position < dataList.size()) {
                BusinessReferenceMainBean.HouseMapBean.DataBean bean = dataList.get(position);
                return getTextSpan(bean.getHouseName(), bean.getHouseMoney(), bean.getHouseMoneyRatio());
            }
            return null;
        }

        private CharSequence getShopTextSpanByPosition(List<BusinessReferenceMainBean.ShopTypeMapBean.DataBean> dataList, int position) {
            if (position < dataList.size()) {
                BusinessReferenceMainBean.ShopTypeMapBean.DataBean bean = dataList.get(position);
                return getTextSpan(bean.getShopType(), bean.getShopMoney(), bean.getShopMoneyRatio());
            }
            return null;
        }
    }

    class PerformanceRatioAloneItem extends MultiPerformanceItem {
        CharSequence yearOnYear;  //同比增长
        CharSequence ringGrowth; //环比增长
        List<BarChartData> barChartData;
        int maxGradient = 100;

        PerformanceRatioAloneItem() {
            super(MultipleItem.TYPE_PERFORMANCE_RATIO_ALONE);
            String total = "-";  //总业绩
            BusinessReferenceMainBean.MultipleMapBean mapBean = mResultMainBean.getChannelMap();
            List<BusinessReferenceMainBean.ChannelMapBean.DataBean> dataList = mResultMainBean.getChannelMap().getData();
            barChartData = new ArrayList<>();
            if (!dataList.isEmpty() && dataList.get(0) != null) {
                BusinessReferenceMainBean.ChannelMapBean.DataBean bean = dataList.get(0);
                total = bean.getTotal();
                String[] array = mContext.getResources().getStringArray(R.array.customer_type);
                theFirst = getTextSpan(array[0], bean.getBag(), bean.getBagRatio()); //拎包入住
                theSecond = getTextSpan(array[1], bean.getHome(), bean.getHomeRatio());  //家装
                theThird = getTextSpan(array[2], bean.getNewRetail(), bean.getNewRetailRatio()); //总部线上引流
                theFourth = getTextSpan(array[3], bean.getConventional(), bean.getConventionalRatio()); //常规</
                theFifth = getTextSpan(array[4], bean.getTraditional(), bean.getTraditionalRatio()); //整装
                theOther = getTextSpan(array[5], bean.getWholesale(), bean.getWholesaleRatio()); //传统渠道
                maxGradient = getMaxGradient(bean.getBagRatioValue(), bean.getHomeRatioValue(), bean.getNewRetailRatioValue(),
                        bean.getConventionalRatioValue(), bean.getWholesaleRatioValue(), bean.getTraditionalRatioValue());
                barChartData.add(new BarChartData("", bean.getBagRatioValue(), mColorArray[0], bean.getBag()));
                barChartData.add(new BarChartData("", bean.getHomeRatioValue(), mColorArray[1], bean.getHome()));
                barChartData.add(new BarChartData("", bean.getNewRetailRatioValue(), mColorArray[2], bean.getNewRetail()));
                barChartData.add(new BarChartData("", bean.getConventionalRatioValue(), mColorArray[3], bean.getConventional()));
                barChartData.add(new BarChartData("", bean.getTraditionalRatioValue(), mColorArray[4], bean.getWholesale()));
                barChartData.add(new BarChartData("", bean.getWholesaleRatioValue(), mColorArray[5], bean.getTraditional()));
            } else {
                barChartData.add(new BarChartData(0, mColorArray[0]));
                barChartData.add(new BarChartData(0, mColorArray[1]));
                barChartData.add(new BarChartData(0, mColorArray[2]));
                barChartData.add(new BarChartData(0, mColorArray[3]));
                barChartData.add(new BarChartData(0, mColorArray[4]));
                barChartData.add(new BarChartData(0, mColorArray[5]));
            }
            String lastYearPercent;
            double lastYearValue;
            String lastMonthPercent;
            double lastMonthValue;
            if (mapBean == null) {
                lastYearPercent = "";
                lastMonthPercent = "";
                lastYearValue = 0;
                lastMonthValue = 0;
            } else {
                lastYearPercent = mapBean.getLastYearPercentText();
                lastMonthPercent = mapBean.getLastMonthPercentText();
                lastYearValue = mapBean.getLastYearPercentValue();
                lastMonthValue = mapBean.getLastMonthPercentValue();
            }
            performance = mTextHelper.obtain((TextUtils.equals(mMonth, "0") || mDatetimeIndex == 0) ? R.string.annual_performance : R.string.this_month_performance, CODE_3000 + total,
                    mColorSpan, mStyleSpan, new AbsoluteSizeSpan(mContext.getResources().getDimensionPixelSize(R.dimen.textSize_16), false));
            ForegroundColorSpan lastYearSpan;
            if (!TextUtils.isEmpty(lastYearPercent)) {
                if (lastYearValue > 0) {
                    lastYearSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.green));
                } else if (lastYearValue < 0) {
                    lastYearSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.bg_homepage_new));
                } else {
                    lastYearSpan = mColorSpan;
                }
            } else {
                lastYearSpan = mColorSpan;
            }
            yearOnYear = mTextHelper.obtain(R.string.growth_year_on_year, CODE_2000 + lastYearPercent, lastYearSpan, mStyleSpan);
            if (!TextUtils.isEmpty(lastMonthPercent)) {
                ForegroundColorSpan lastMonthSpan;
                if (lastMonthValue > 0) {
                    lastMonthSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.green));
                } else if (lastMonthValue < 0) {
                    lastMonthSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.bg_homepage_new));
                } else {
                    lastMonthSpan = mColorSpan;
                }
                ringGrowth = mTextHelper.obtain(R.string.growth_in_the_ring, CODE_2000 + lastMonthPercent, lastMonthSpan, mStyleSpan);
            } else {
                ringGrowth = null;
            }
        }

        private int getMaxGradient(double... values) {
            double max = 0;
            for (double value : values) {
                max = Math.max(max, value);
            }
            if (max > 100) {
                return getMaxGraded(max);
            }
            return 100;
        }

        private int getMaxGraded(double value) {
            int i = 1;
            for (; value > 10; ) {
                value = value / 10;
                i = i * 10;
            }
            return (int) (i * (Math.ceil(value)));
        }
    }

    /*花色业绩*/
    class ColorPerformanceItem extends MultiPerformanceItem {
        List<PieChartData> pieChartData;

        ColorPerformanceItem(int index) {
            super(ColorPerformanceItem.TYPE_COLOR_PERFORMANCE);
            setTabIndex(index);
        }

        /*还原值*/
        void revert() {
            theFirst = null;
            theSecond = null;
            theThird = null;
            theFourth = null;
            theFifth = null;
            theOther = null;
            if (pieChartData != null) {
                pieChartData.clear();
                pieChartData = null;
            }
        }

        void setTabIndex(int index) {
            revert();
            BusinessReferenceMainBean.ColorMapBean.ColorMapInnerBean bean;
            if (index == 0) { //定制
                bean = mResultMainBean.getColorMap().getOrderMap();
            } else if (index == 1) { //橱柜
                bean = mResultMainBean.getColorMap().getCupboardMap();
            } else { //木门
                bean = mResultMainBean.getColorMap().getWoodDoorMap();
            }
            performance = mTextHelper.obtain(R.string.top5_performance, "\n" + bean.getSum(), mStyleSpan, mColorSpan, mSizeSpan);
            List<BusinessReferenceMainBean.ColorMapBean.ColorMapInnerBean.DataBean> dataList = bean.getData();
            theFirst = getTextSpanByPosition(dataList, 0);
            theSecond = getTextSpanByPosition(dataList, 1);
            theThird = getTextSpanByPosition(dataList, 2);
            theFourth = getTextSpanByPosition(dataList, 3);
            theFifth = getTextSpanByPosition(dataList, 4);
            theOther = getTextSpanByPosition(dataList, 5);
            pieChartData = new ArrayList<>();
            for (int i = 0; i < mColorArray.length; i++) {
                if (i >= dataList.size()) {
                    break;
                } else {
                    pieChartData.add(new PieChartData(dataList.get(i).getColorMoneyValue(), mColorArray[i]));
                }
            }
        }

        private CharSequence getTextSpanByPosition(List<BusinessReferenceMainBean.ColorMapBean.ColorMapInnerBean.DataBean> dataList, int position) {
            if (position < dataList.size()) {
                BusinessReferenceMainBean.ColorMapBean.ColorMapInnerBean.DataBean bean = dataList.get(position);
                return getTextSpan(bean.getColorName(), bean.getColorMoney(), bean.getColorMoneyRatio());
            }
            return null;
        }
    }

    /*系列业绩*/
    class SeriesPerformanceItem extends MultiPerformanceItem {
        List<PieChartData> pieChartData;

        SeriesPerformanceItem(int index) {
            super(ColorPerformanceItem.TYPE_SERIES_PERFORMANCE);
            setTabIndex(index);
        }

        /*还原值*/
        void revert() {
            theFirst = null;
            theSecond = null;
            theThird = null;
            theFourth = null;
            theFifth = null;
            theOther = null;
            if (pieChartData != null) {
                pieChartData.clear();
                pieChartData = null;
            }
        }

        void setTabIndex(int index) {
            revert();
            BusinessReferenceMainBean.SeriesMapBean.SeriesMapInnerBean bean;
            if (index == 0) { //定制
                bean = mResultMainBean.getSeriesMap().getOrderMap();
            } else if (index == 1) { //橱柜
                bean = mResultMainBean.getSeriesMap().getCupboardMap();
            } else { //木门
                bean = mResultMainBean.getSeriesMap().getWoodDoorMap();
            }
            performance = mTextHelper.obtain(R.string.top5_performance, "\n" + bean.getSum(), mStyleSpan, mColorSpan, mSizeSpan);
            List<BusinessReferenceMainBean.SeriesMapBean.SeriesMapInnerBean.DataBean> dataList = bean.getData();
            theFirst = getTextSpanByPosition(dataList, 0);
            theSecond = getTextSpanByPosition(dataList, 1);
            theThird = getTextSpanByPosition(dataList, 2);
            theFourth = getTextSpanByPosition(dataList, 3);
            theFifth = getTextSpanByPosition(dataList, 4);
            theOther = getTextSpanByPosition(dataList, 5);
            pieChartData = new ArrayList<>();
            for (int i = 0; i < mColorArray.length; i++) {
                if (i >= dataList.size()) {
                    break;
                } else {
                    pieChartData.add(new PieChartData(dataList.get(i).getStyleMoneyValue(), mColorArray[i]));
                }
            }
        }

        private CharSequence getTextSpanByPosition(List<BusinessReferenceMainBean.SeriesMapBean.SeriesMapInnerBean.DataBean> dataList, int position) {
            if (position < dataList.size()) {
                BusinessReferenceMainBean.SeriesMapBean.SeriesMapInnerBean.DataBean bean = dataList.get(position);
                return getTextSpan(bean.getStyleName(), bean.getStyleMoney(), bean.getStyleMoneyRatio());
            }
            return null;
        }
    }

    /*客户来源*/
    class CustomerSourceItem extends MultiPerformanceItem {
        CharSequence yearOnYear;  //同比增长
        CharSequence ringGrowth; //环比增长
        List<PieChartData> pieChartData;

        CustomerSourceItem(int index) {
            super(MultipleItem.TYPE_CUSTOMER_SOURCE);
            setTabIndex(index);
        }

        /*还原值*/
        void revert() {
            yearOnYear = null;
            ringGrowth = null;
            theFirst = null;
            theSecond = null;
            theThird = null;
            theFourth = null;
            theFifth = null;
            theOther = null;
            if (pieChartData != null) {
                pieChartData.clear();
                pieChartData = null;
            }
        }

        void setTabIndex(int index) {
            revert();
            BusinessReferenceMainBean.CustomerShopMapBean bean;
            if (index == 0) {
                bean = mResultMainBean.getInShopCustomerMap();
            } else {
                bean = mResultMainBean.getOverDealCustomerMap();
            }
            pieChartData = new ArrayList<>();
            List<BusinessReferenceMainBean.CustomerShopMapBean.DataBean> dataList = bean.getData();
            for (int i = 0; i < 6; i++) {
                if (i < dataList.size()) {
                    pieChartData.add(new PieChartData(dataList.get(i).getCountLong(), mColorArray[i]));
                }
            }
            performance = mTextHelper.obtain(index == 0 ? R.string.number_of_new_customers : R.string.number_of_contracted_customer, "\n" + bean.getSum(), mColorSpan, mStyleSpan, mSizeSpan);
            ForegroundColorSpan yearColorSpan;
            String lastYearPercentText = bean.getLastYearPercentText();
            String lastYearTextApp = lastYearPercentText;
            if (!TextUtils.isEmpty(lastYearPercentText)) {
                double lastYearValue = bean.getLastYearPercentValue();
                if (lastYearValue > 0) {
                    yearColorSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.green));
                    lastYearTextApp = "+" + lastYearPercentText;
                } else if (lastYearValue < 0) {
                    yearColorSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.bg_homepage_new));
                } else {
                    yearColorSpan = mColorSpan;
                }
            } else {
                yearColorSpan = mColorSpan;
            }
            yearOnYear = mTextHelper.obtain(R.string.growth_year_on_year, CODE_2000 + lastYearTextApp, yearColorSpan, mStyleSpan);
            String lastMonthPercentText = bean.getLastMonthPercentText();
            if (!TextUtils.isEmpty(lastMonthPercentText)) {
                ForegroundColorSpan lastMonthSpan;
                String lastMonthTextApp = lastMonthPercentText;
                double lastMonthValue = bean.getLastMonthPercentValue();
                if (lastMonthValue > 0) {
                    lastMonthSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.green));
                    lastMonthTextApp = "+" + lastMonthPercentText;
                } else if (lastMonthValue < 0) {
                    lastMonthSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.bg_homepage_new));
                } else {
                    lastMonthSpan = mColorSpan;
                }
                ringGrowth = mTextHelper.obtain(R.string.growth_in_the_ring, CODE_2000 + lastMonthTextApp, lastMonthSpan, mStyleSpan);
            } else {
                ringGrowth = null;
            }
            theFirst = getTextSpanByPosition(dataList, 0);
            theSecond = getTextSpanByPosition(dataList, 1);
            theThird = getTextSpanByPosition(dataList, 2);
            theFourth = getTextSpanByPosition(dataList, 3);
            theFifth = getTextSpanByPosition(dataList, 4);
            theOther = getTextSpanByPosition(dataList, 5);
        }

        private CharSequence getTextSpanByPosition(List<BusinessReferenceMainBean.CustomerShopMapBean.DataBean> dataList, int position) {
            if (position < dataList.size()) {
                BusinessReferenceMainBean.CustomerShopMapBean.DataBean bean = dataList.get(position);
                return getTextSpan(bean.getSourceName(), bean.getCount(), bean.getCountRatio());
            }
            return null;
        }
    }

    /*客户分析item*/
    class CustomerAnalysisItem extends MultipleItem {
        CharSequence customerPrice; //客单价
        int customerPriceDrawableId;
        CharSequence period; //平均成交周期
        int periodDrawableId;
        CharSequence signOrder; //签约-下单
        CharSequence orderInstall; //下单-已安装

        CustomerAnalysisItem(BusinessReferenceMainBean.CustomerDetailBean bean) {
            super(MultipleItem.TYPE_CUSTOMER_ANALYSIS);
            //客单价和上月对比(>0 比上月提高，=0和上月一样，<0比上月下降)
            ForegroundColorSpan colorSpan;
            if (bean.getCustomerPriceCompare() > 0) {
                colorSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.green));
                customerPriceDrawableId = R.drawable.ic_rise;
            } else if (bean.getCustomerPriceCompare() < 0) {
                colorSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.bg_homepage_new));
                customerPriceDrawableId = R.drawable.ic_decline;
            } else {
                colorSpan = mColorSpan;
                customerPriceDrawableId = 0;
            }
            customerPrice = mTextHelper.obtain(R.string.customer_price, CODE_3000 + bean.getCustomerPrice(), colorSpan, mStyleSpan); //客单价
            ForegroundColorSpan periodColorSpan;
            if (bean.getDealDayCompare() > 0) {
                periodColorSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.green));
                periodDrawableId = R.drawable.ic_rise;
            } else if (bean.getCustomerPriceCompare() < 0) {
                periodColorSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.bg_homepage_new));
                periodDrawableId = R.drawable.ic_decline;
            } else {
                periodColorSpan = null;
                periodDrawableId = 0;
            }
            period = mTextHelper.obtain(R.string.average_turnover_period2, CODE_3000 + bean.getDealDay(), periodColorSpan, mStyleSpan);
            signOrder = getTextSpan(R.string.signing, R.string.order, bean.getOrderDay());
            orderInstall = getTextSpan(R.string.order, R.string.installed, bean.getInstallDay());
        }

        SpannableString getTextSpan(int leftId, int rightId, String content) {
            String source = mContext.getString(leftId);
            source += " [image] " + mContext.getString(rightId);
            int start = source.length();
            source += CODE_3000 + content;
            SpannableString ss = new SpannableString(source);
            ss.setSpan(new CenterAlignImageSpan(mContext, R.drawable.ic_step_right), source.indexOf("["), source.indexOf("]") + 1, FLAGS);
            ss.setSpan(mStyleSpan, start, source.length(), FLAGS);
            return ss;
        }
    }

    /*客户满意度item*/
    class CustomerSatisfactionItem extends MultipleItem {
        CharSequence satisfaction;  //客户满意度
        int drawableId;
        CharSequence service;  //服务满意度
        CharSequence delivery; //配送满意度
        CharSequence design; //设计满意度
        CharSequence install; //安装满意度

        CustomerSatisfactionItem(BusinessReferenceMainBean.CustomerSatisfactionMapBean bean) {
            super(CustomerSatisfactionItem.TYPE_CUSTOMER_SATISFACTION);
            String source = mContext.getString(R.string.customer_satisfaction_tips);
            int start = source.length();
            source += CODE_3000 + bean.getData().getEvaluate() + " ";
            int end = source.length();
            source += "[star]";
            int startEnd = source.length();
            source += CODE_2000 + ((bean.getEvaluateCompare() > 0 || bean.getEvaluateCompare() < 0) ? "@image" : "");
            SpannableString ss = new SpannableString(source);
            ss.setSpan(mStyleSpan, start, end, FLAGS);
            ss.setSpan(new CenterAlignImageSpan(mContext, R.drawable.ic_star), end, startEnd, FLAGS);
            //>0 比上月提高，=0和上月一样，<0比上月下降
            if (bean.getEvaluateCompare() > 0) {
                ss.setSpan(new CenterAlignImageSpan(mContext, R.drawable.ic_rise), source.lastIndexOf("@"), source.length(), FLAGS);
            } else if (bean.getEvaluateCompare() < 0) {
                ss.setSpan(new CenterAlignImageSpan(mContext, R.drawable.ic_decline), source.lastIndexOf("@"), source.length(), FLAGS);
            }
            satisfaction = ss;
            drawableId = R.drawable.ic_star_gray;
            service = mTextHelper.obtain(R.string.customer_satisfaction_service2, CODE_3000 + bean.getData().getService());
            delivery = mTextHelper.obtain(R.string.customer_satisfaction_delivery2, CODE_3000 + bean.getData().getDelivery());
            design = mTextHelper.obtain(R.string.customer_satisfaction_design2, CODE_3000 + bean.getData().getDesign());
            install = mTextHelper.obtain(R.string.customer_satisfaction_install2, CODE_3000 + bean.getData().getInstall());
        }
    }

    /*门店效益item*/
    class ShopBenefitItem extends MultipleItem {
        CharSequence effect; //坪效
        int effectDrawableId;
        CharSequence totalEffect; //总人效
        int userEffectDrawableId;
        CharSequence salesEffect; //销售人效
        int salesEffectDrawableId;

        ShopBenefitItem(BusinessReferenceMainBean.ShopDetailBean bean) {
            super(MultipleItem.TYPE_SHOP_BENEFIT);
            ForegroundColorSpan areaBenefitSpan;
            if (bean.getAreaBenefitCompare() > 0) {
                areaBenefitSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.green));
                effectDrawableId = R.drawable.ic_rise;
            } else if (bean.getAreaBenefitCompare() < 0) {
                areaBenefitSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.bg_homepage_new));
                effectDrawableId = R.drawable.ic_decline;
            } else {
                areaBenefitSpan = null;
                effectDrawableId = 0;
            }
            effect = mTextHelper.obtain(R.string.effect, CODE_2000 + bean.getAreaBenefit(), mStyleSpan, areaBenefitSpan);
            ForegroundColorSpan userBenefitSpan;
            if (bean.getUserBenefitCompare() > 0) {
                userBenefitSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.green));
                userEffectDrawableId = R.drawable.ic_rise;
            } else if (bean.getUserBenefitCompare() < 0) {
                userBenefitSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.bg_homepage_new));
                userEffectDrawableId = R.drawable.ic_decline;
            } else {
                userBenefitSpan = null;
                userEffectDrawableId = 0;
            }
            totalEffect = mTextHelper.obtain(R.string.total_human_effect, CODE_2000 + bean.getUserBenefit(), mStyleSpan, userBenefitSpan);
            ForegroundColorSpan salesBenefitSpan;
            if (bean.getSalesmanBenefitCompare() > 0) {
                salesBenefitSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.green));
                salesEffectDrawableId = R.drawable.ic_rise;
            } else if (bean.getSalesmanBenefitCompare() < 0) {
                salesBenefitSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.bg_homepage_new));
                salesEffectDrawableId = R.drawable.ic_decline;
            } else {
                salesBenefitSpan = null;
                salesEffectDrawableId = 0;
            }
            salesEffect = mTextHelper.obtain(R.string.sales_effectiveness, CODE_2000 + bean.getSalesmanBenefit(), mStyleSpan, salesBenefitSpan);
        }
    }

    /*盈利状况item-延期*/
//    class ProfitabilityItem extends MultipleItem {
//        CharSequence payback; //回款
//        boolean isPaybackRise = false;
//        CharSequence grossProfit; //毛利
//        CharSequence grossProfitRate; //毛利率
//        CharSequence growthLeft; //左边-环比增长
//        CharSequence operatingCost; //运营成本
//        CharSequence netIncome; //净利
//        boolean isNetIncomeRise = true;
//        CharSequence netIncomeRate; //净利率
//        boolean isNetIncomeRateRise = true;
//        CharSequence growthRight; //右边-环比增长
//        boolean isGrowthRightRise = true;
//
//        ProfitabilityItem() {
//            super(MultipleItem.TYPE_PROFITABILITY);
//            payback = mTextHelper.obtain(R.string.payback, CODE_3000 + "89.56万", mStyleSpan,
//                    new ForegroundColorSpan(getColor(isPaybackRise)));
//            grossProfit = mTextHelper.obtain(R.string.gross_profit, CODE_3000 + "26.5万", mStyleSpan);
//            grossProfitRate = mTextHelper.obtain(R.string.gross_profit_rate, CODE_3000 + "76.82%", mStyleSpan);
//            growthLeft = mTextHelper.obtain(R.string.growth_in_the_ring, CODE_3000 + "10.18%", mStyleSpan);
//            operatingCost = mTextHelper.obtain(R.string.operating_cost, CODE_3000 + "35.62万", mStyleSpan);
//            netIncome = mTextHelper.obtain(R.string.net_income, CODE_3000 + "38.68万", mStyleSpan,
//                    new ForegroundColorSpan(getColor(isNetIncomeRise)));
//            netIncomeRate = mTextHelper.obtain(R.string.net_income_rate, CODE_3000 + "78.26%", mStyleSpan,
//                    new ForegroundColorSpan(getColor(isNetIncomeRateRise)));
//            growthRight = mTextHelper.obtain(R.string.growth_in_the_ring, CODE_3000 + "10.18%", mStyleSpan,
//                    new ForegroundColorSpan(getColor(isGrowthRightRise)));
//        }
//    }

    /*经销商排名-数据排名*/
    class DealerRankingOneItem extends MultipleItem {
        CharSequence performance; //业绩
        CharSequence turnoverRate; //进店成交率
        CharSequence effect; //坪效
        CharSequence customerPrice; //客单价
        CharSequence scaleRate; //量尺成交率
        CharSequence humanEffect; //人效

        ForegroundColorSpan colorSpan;
        AbsoluteSizeSpan sizeSpan;
        StyleSpan styleSpan;

        DealerRankingOneItem(BusinessReferenceMainBean.DealerRankingMapBean bean) {
            super(MultipleItem.TYPE_DEALER_RANKING_ONE);
            colorSpan = new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.ring_tow));
            sizeSpan = new AbsoluteSizeSpan(mContext.getResources().getDimensionPixelSize(R.dimen.textSize_15), false);
            styleSpan = new StyleSpan(Typeface.BOLD);
            performance = getRanking(R.string.performance_ranking, bean.getPerformance());
            turnoverRate = getRanking(R.string.entry_store_rate_ranking, bean.getEntryRatioRank());
            effect = getRanking(R.string.effect_ranking, bean.getEffectRank());
            customerPrice = getRanking(R.string.customer_price_ranking, bean.getCustomerPrice());
            scaleRate = getRanking(R.string.scale_turnover_rate_ranking, bean.getScaleRatioRank());
            humanEffect = getRanking(R.string.human_effect_ranking, bean.getHumanEffectRank());
        }

        SpannableString getRanking(int originId, String value) {
            String source = mContext.getString(originId) + "\n";
            int start = source.length();
            source += (TextUtils.isEmpty(value) ? "" : value);
            int end = source.length();
            SpannableString ss = new SpannableString(source);
            int flags = FLAGS;
            ss.setSpan(colorSpan, start, end, flags);
            ss.setSpan(sizeSpan, start, end, flags);
            ss.setSpan(styleSpan, start, end, flags);
            return ss;
        }
    }

    class DealerRankingSecondItem extends MultipleItem {
        List<LineChartData> dataList;

        DealerRankingSecondItem(List<String> list) {
            super(MultipleItem.TYPE_DEALER_RANKING_TWO);
            dataList = new ArrayList<>();
            for (String value : list) {
                dataList.add(new LineChartData("", ParseUtils.parseLong(value), "排名：" + value));
            }
        }
    }

    class DealerRankingThirdItem extends MultipleItem {
        //默认全灰色图片
        int currentPosition;
        int dangerDrawableId = R.drawable.ic_danger_gray;  //危险区图片id
        int striveDrawableId = R.drawable.ic_strive_gray;   //努力区图片id
        int potentialDrawableId = R.drawable.ic_potential_gray; //潜力区图片id
        int excellentDrawableId = R.drawable.ic_excellent_gray; //优秀区图片id
        int outstandingDrawableId = R.drawable.ic_outstanding_gray;  //卓越区图片id
        int dangerTextColor = ContextCompat.getColor(mContext, R.color.textColor6);
        int striveTextColor = ContextCompat.getColor(mContext, R.color.textColor6);
        int potentialTextColor = ContextCompat.getColor(mContext, R.color.textColor6);
        int excellentTextColor = ContextCompat.getColor(mContext, R.color.textColor6);
        int outstandingTextColor = ContextCompat.getColor(mContext, R.color.textColor6);
        int leftMargin;

        DealerRankingThirdItem(BusinessReferenceMainBean.DealerRankingMapBean.LevelBean bean) {
            super(MultipleItem.TYPE_DEALER_RANKING_THREE);
            //1-卓越区；2-优秀区；3-潜力区；4-努力区；5-危险区；)
            int screenWidth = DensityUtil.getScreenWidth(mContext);
            int itemWidth = (screenWidth - mContext.getResources().getDimensionPixelSize(R.dimen.dp_18) * 2) / 5;
            leftMargin = (int) ((itemWidth - mContext.getResources().getDimensionPixelSize(R.dimen.dp_16)) * bean.getPosition());
            String level = bean.level;
            if (TextUtils.equals(level, "1")) {
                outstandingDrawableId = R.drawable.ic_outstanding;
                outstandingTextColor = ContextCompat.getColor(mContext, R.color.pos_outstanding);
                currentPosition = 5;
            } else if (TextUtils.equals(level, "2")) {
                excellentDrawableId = R.drawable.ic_excellent;
                excellentTextColor = ContextCompat.getColor(mContext, R.color.pos_excellent);
                currentPosition = 4;
            } else if (TextUtils.equals(level, "3")) {
                potentialDrawableId = R.drawable.ic_potential;
                potentialTextColor = ContextCompat.getColor(mContext, R.color.pos_potential);
                currentPosition = 3;
            } else if (TextUtils.equals(level, "4")) {
                striveDrawableId = R.drawable.ic_strive;
                striveTextColor = ContextCompat.getColor(mContext, R.color.pos_strive);
                currentPosition = 2;
            } else if (TextUtils.equals(level, "5")) {
                dangerDrawableId = R.drawable.ic_danger;
                dangerTextColor = ContextCompat.getColor(mContext, R.color.pos_danger);
                currentPosition = 1;
            }
        }
    }

    class DealerRankingFourItem extends MultipleItem {
        String top1Name;
        String top2Name;
        String top3Name;

        DealerRankingFourItem(BusinessReferenceMainBean.DealerRankingMapBean bean) {
            super(MultipleItem.TYPE_DEALER_RANKING_FOUR);
            top1Name = bean.getTop1DealerName();
            top2Name = bean.getTop2DealerName();
            top3Name = bean.getTop3DealerName();
        }
    }

    private final class MultipleItemAdapter extends CommonAdapter<MultipleItem> {

        MultipleItemAdapter(Context context, List<MultipleItem> dataList) {
            super(context, dataList);
        }

        @Override
        public int getItemViewType(int position) {
            return mDatas.get(position).getItemType();
        }

        @Override
        protected int bindView(int viewType) {
            if (viewType == MultipleItem.TYPE_PERFORMANCE_RATIO_TITLE ||
                    viewType == MultipleItem.TYPE_COLOR_PERFORMANCE_TITLE ||
                    viewType == MultipleItem.TYPE_SERIES_PERFORMANCE_TITLE ||
                    viewType == MultipleItem.TYPE_CUSTOMER_SOURCE_TITLE ||
                    viewType == MultipleItem.TYPE_CUSTOMER_ANALYSIS_TITLE ||
                    viewType == MultipleItem.TYPE_CUSTOMER_SATISFACTION_TITLE ||
                    viewType == MultipleItem.TYPE_SHOP_BENEFIT_TITLE ||
                    viewType == MultipleItem.TYPE_PROFITABILITY_TITLE ||
                    viewType == MultipleItem.TYPE_DEALER_RANKING_TITLE) {
                return R.layout.item_business_reference_main_title;
            } else {
                if (viewType == MultipleItem.TYPE_PERFORMANCE_RATIO_TAB) {
                    return R.layout.item_business_reference_performanceratio_tabhost;
                } else if (viewType == MultipleItem.TYPE_PERFORMANCE_RATIO) {
                    return R.layout.item_business_reference_performanceratio;
                } else if (viewType == MultipleItem.TYPE_PERFORMANCE_RATIO_ALONE) {
                    return R.layout.item_business_reference_performanceratio_alone;
                } else if (viewType == MultipleItem.TYPE_COLOR_PERFORMANCE) {
                    return R.layout.item_business_reference_colorperformance;
                } else if (viewType == MultipleItem.TYPE_SERIES_PERFORMANCE) {
                    return R.layout.item_business_reference_seriesperformance;
                } else if (viewType == MultipleItem.TYPE_CUSTOMER_SOURCE) {
                    return R.layout.item_business_reference_customer_source;
                } else if (viewType == MultipleItem.TYPE_CUSTOMER_ANALYSIS) {
                    return R.layout.item_business_reference_customeranalysis;
                } else if (viewType == MultipleItem.TYPE_CUSTOMER_SATISFACTION) {
                    return R.layout.item_business_reference_customersatisfaction;
                } else if (viewType == MultipleItem.TYPE_SHOP_BENEFIT) {
                    return R.layout.item_business_reference_shopbenefit;
                } else if (viewType == MultipleItem.TYPE_PROFITABILITY) {
                    return R.layout.item_business_reference_profitability;
                } else if (viewType == MultipleItem.TYPE_DEALER_RANKING_ONE) {
                    return R.layout.item_business_reference_dealerranking_one;
                } else if (viewType == MultipleItem.TYPE_DEALER_RANKING_TWO) {
                    return R.layout.item_business_reference_dealerranking_two;
                } else if (viewType == MultipleItem.TYPE_DEALER_RANKING_THREE) {
                    return R.layout.item_business_reference_dealerranking_three;
                } else {
                    return R.layout.item_business_reference_dealerranking_four;
                }
            }
        }

        @Override
        public void onBindHolder(RecyclerHolder holder, MultipleItem multipleItem, int position) {
            int itemType = holder.getItemViewType();
            if (itemType == MultipleItem.TYPE_PERFORMANCE_RATIO_TITLE ||
                    itemType == MultipleItem.TYPE_COLOR_PERFORMANCE_TITLE ||
                    itemType == MultipleItem.TYPE_SERIES_PERFORMANCE_TITLE ||
                    itemType == MultipleItem.TYPE_CUSTOMER_SOURCE_TITLE ||
                    itemType == MultipleItem.TYPE_CUSTOMER_ANALYSIS_TITLE ||
                    itemType == MultipleItem.TYPE_CUSTOMER_SATISFACTION_TITLE ||
                    itemType == MultipleItem.TYPE_SHOP_BENEFIT_TITLE ||
                    itemType == MultipleItem.TYPE_PROFITABILITY_TITLE ||
                    itemType == MultipleItem.TYPE_DEALER_RANKING_TITLE) {
                holder.setText(R.id.tv_title, multipleItem.title);
                holder.setText(R.id.tv_right_menu, multipleItem.menu);
                holder.setEnabled(R.id.tv_right_menu, !TextUtils.isEmpty(multipleItem.menu));
                holder.setOnClickListener(R.id.tv_right_menu, view -> onMenuClick(itemType));
            } else if (itemType == MultipleItem.TYPE_PERFORMANCE_RATIO_TAB) {
                setPerformanceTab(holder);
            } else if (itemType == MultipleItem.TYPE_PERFORMANCE_RATIO) {  //业绩占比
                PerformanceRatioItem item = (PerformanceRatioItem) multipleItem;
                setPerformanceRatio(holder, item);
            } else if (itemType == MultipleItem.TYPE_PERFORMANCE_RATIO_ALONE) {
                PerformanceRatioAloneItem item = (PerformanceRatioAloneItem) multipleItem;
                setPerformanceRatioAlone(holder, item);
            } else if (itemType == MultipleItem.TYPE_COLOR_PERFORMANCE) { //花色业绩
                ColorPerformanceItem item = (ColorPerformanceItem) multipleItem;
                setColorPerformance(holder, item, position);
            } else if (itemType == MultipleItem.TYPE_SERIES_PERFORMANCE) { //系列业绩
                SeriesPerformanceItem item = (SeriesPerformanceItem) multipleItem;
                setSeriesPerformance(holder, item, position);
            } else if (itemType == MultipleItem.TYPE_CUSTOMER_SOURCE) { //客户来源
                CustomerSourceItem item = (CustomerSourceItem) multipleItem;
                setCustomerSource(holder, item, position);
            } else if (itemType == MultipleItem.TYPE_CUSTOMER_ANALYSIS) { //客户分析
                CustomerAnalysisItem item = (CustomerAnalysisItem) multipleItem;
                setCustomerAnalysis(holder, item);
            } else if (itemType == MultipleItem.TYPE_CUSTOMER_SATISFACTION) { //客户满意度
                CustomerSatisfactionItem item = (CustomerSatisfactionItem) multipleItem;
                setCustomerSatisfaction(holder, item);
            } else if (itemType == MultipleItem.TYPE_SHOP_BENEFIT) { //门店效益
                ShopBenefitItem item = (ShopBenefitItem) multipleItem;
                setShopBenefit(holder, item);
            }
//            else if (itemType == MultipleItem.TYPE_PROFITABILITY) { //盈利状况
//                ProfitabilityItem item = (ProfitabilityItem) multipleItem;
//                setProfitability(holder, item);
//            }
            else if (itemType == MultipleItem.TYPE_DEALER_RANKING_ONE) {  //经销商排名-城市排名
                DealerRankingOneItem item = (DealerRankingOneItem) multipleItem;
                setDealerRankingOne(holder, item);
            } else if (itemType == MultipleItem.TYPE_DEALER_RANKING_TWO) {
                DealerRankingSecondItem item = (DealerRankingSecondItem) multipleItem;
                setDealerRankingTwo(holder, item);
            } else if (itemType == MultipleItem.TYPE_DEALER_RANKING_THREE) {
                DealerRankingThirdItem item = (DealerRankingThirdItem) multipleItem;
                setDealerRankingThird(holder, item);
            } else if (itemType == MultipleItem.TYPE_DEALER_RANKING_FOUR) {
                DealerRankingFourItem item = (DealerRankingFourItem) multipleItem;
                setDealerRankingFour(holder, item);
            }
        }

        private void onMenuClick(int itemType) {
            if (itemType == MultipleItem.TYPE_COLOR_PERFORMANCE_TITLE) {  //更多花色业绩
                DealerMultiPerformanceActivity.renderColors((BaseActivity<?, ?>) mContext);
            } else if (itemType == MultipleItem.TYPE_SERIES_PERFORMANCE_TITLE) { //更多系列业绩
                DealerMultiPerformanceActivity.renderSeries((BaseActivity<?, ?>) mContext);
            } else if (itemType == MultipleItem.TYPE_PERFORMANCE_RATIO_TITLE) { // 点击品类的时候，下方会出现详细宅配报表，选择其他维度业绩时，无详细宅配报表出现
                DealerMultiPerformanceActivity.renderHomeDelivery((BaseActivity<?, ?>) mContext);
            } else if (itemType == MultipleItem.TYPE_CUSTOMER_ANALYSIS_TITLE) { //客户分析-数据说明
                new PlainTextTipsDialog(mContext).setData(R.array.business_reference_customer_analysis_tips).show();
            } else if (itemType == MultipleItem.TYPE_SHOP_BENEFIT_TITLE) {  //门店效益-数据说明
                new PlainTextTipsDialog(mContext).setData(R.array.business_reference_shop_benefit_tips).show();
            }
        }

        private void setPerformanceTab(final RecyclerHolder holder) {
            if (mPerformanceTab == 0) {
                setSelected(holder, R.id.tv_plate);
            } else if (mPerformanceTab == 1) {
                setSelected(holder, R.id.tv_category);
            } else if (mPerformanceTab == 2) {
                setSelected(holder, R.id.tv_space);
            } else if (mPerformanceTab == 3) {
                setSelected(holder, R.id.tv_customer);
            } else {
                setSelected(holder, R.id.tv_shop);
            }
            holder.setOnClickListener(R.id.tv_plate, view -> onPerformanceTabChange(0, holder, R.id.tv_plate, false));
            //点击品类的时候，下方会出现详细宅配报表，选择其他维度业绩时，无详细宅配报表出现
            holder.setOnClickListener(R.id.tv_category, view -> onPerformanceTabChange(1, holder, R.id.tv_category, true));
            holder.setOnClickListener(R.id.tv_space, view -> onPerformanceTabChange(2, holder, R.id.tv_space, false));
            holder.setOnClickListener(R.id.tv_customer, view -> onPerformanceTabChange(3, holder, R.id.tv_customer, false));
            holder.setOnClickListener(R.id.tv_shop, view -> onPerformanceTabChange(4, holder, R.id.tv_shop, false));
        }

        private void onPerformanceTabChange(int tabIndex, RecyclerHolder holder, int selectId, boolean show) {
            if (mPerformanceTab != tabIndex) {
                mPerformanceTab = tabIndex;
                setSelected(holder, selectId);
                if (mPerformanceTab == 3) {
                    mDatas.set(2, new PerformanceRatioAloneItem());
                } else {
                    mDatas.set(2, new PerformanceRatioItem(mPerformanceTab));
                }
                if (show) {  //显示“详细宅配报表”菜单按钮
                    mDatas.set(0, new MultipleItem(MultipleItem.TYPE_PERFORMANCE_RATIO_TITLE,
                            mContext.getString(R.string.performance_ratio),
                            mContext.getString(R.string.more_homedelivery_arrow)));
                } else {    //否则隐藏
                    mDatas.set(0, new MultipleItem(MultipleItem.TYPE_PERFORMANCE_RATIO_TITLE,
                            mContext.getString(R.string.performance_ratio), ""));
                }
                notifyDataSetChanged();
            }
        }

        /*业绩占比*/
        private void setPerformanceRatio(final RecyclerHolder holder, final PerformanceRatioItem item) {
            holder.setVisibility(R.id.fl_pie_layout, View.VISIBLE);
            PieChartView pieChart = holder.obtainView(R.id.pieChart);
            pieChart.setPieChartDataList(item.pieChartData);
            holder.setText(R.id.tv_performance, item.performance); //本月业绩
            holder.setText(R.id.tv_yoy_growth, item.yearOnYear);   //同比增长
            if (TextUtils.isEmpty(item.ringGrowth)) {
                holder.setVisibility(R.id.tv_ring_growth, View.GONE);
            } else {
                holder.setVisibility(R.id.tv_ring_growth, View.VISIBLE);
                holder.setText(R.id.tv_ring_growth, item.ringGrowth);   //环比增长
            }
            setMultiPerformance(holder, item, false);
        }

        /*业绩占比-客户类型*/
        private void setPerformanceRatioAlone(final RecyclerHolder holder, final PerformanceRatioAloneItem item) {
            BarChartView barChart = holder.obtainView(R.id.barChart);
            barChart.withData(item.barChartData, null, "%").maxGradient(item.maxGradient)
                    .barValueTypeface(Typeface.DEFAULT_BOLD).start();
            holder.setText(R.id.tv_performance, item.performance); //本月业绩
            holder.setText(R.id.tv_yoy_growth, item.yearOnYear);   //同比增长
            if (TextUtils.isEmpty(item.ringGrowth)) {
                holder.setVisibility(R.id.tv_ring_growth, View.GONE);
            } else {
                holder.setVisibility(R.id.tv_ring_growth, View.VISIBLE);
                holder.setText(R.id.tv_ring_growth, item.ringGrowth);   //环比增长
            }
            setMultiPerformance(holder, item, false);
        }

        /*花色业绩*/
        private void setColorPerformance(RecyclerHolder holder, ColorPerformanceItem item, int position) {
            if (mColorsTab == 0) {
                setSelected(holder, R.id.tv_custom);
            } else if (mColorsTab == 1) {
                setSelected(holder, R.id.tv_cupboard);
            } else {
                setSelected(holder, R.id.tv_wooden_door);
            }
            holder.setOnClickListener(R.id.tv_custom, view -> onColorsTabChange(position, item, 0, holder, R.id.tv_custom));  //选中“定制”
            holder.setOnClickListener(R.id.tv_cupboard, view -> onColorsTabChange(position, item, 1, holder, R.id.tv_cupboard)); //选中“橱柜”
            holder.setOnClickListener(R.id.tv_wooden_door, view -> onColorsTabChange(position, item, 2, holder, R.id.tv_wooden_door)); //选中“木门”
            holder.setText(R.id.tv_performance, item.performance);  //TOP5业绩
            PieChartView pieChart = holder.obtainView(R.id.pieChart);
            pieChart.setPieChartDataList(item.pieChartData);
            setMultiPerformance(holder, item, true);
        }

        private void onColorsTabChange(int position, ColorPerformanceItem item, int tabIndex, RecyclerHolder holder, int selectId) {
            if (mColorsTab != tabIndex) {
                mColorsTab = tabIndex;
                setSelected(holder, selectId);
                item.setTabIndex(mColorsTab);
                mDatas.set(position, item);  //花色业绩index为3
                notifyDataSetChanged();
            }
        }

        /*系列业绩*/
        private void setSeriesPerformance(RecyclerHolder holder, SeriesPerformanceItem item, int position) {
            if (mSeriesTab == 0) {
                setSelected(holder, R.id.tv_custom);
            } else if (mSeriesTab == 1) {
                setSelected(holder, R.id.tv_cupboard);
            } else {
                setSelected(holder, R.id.tv_wooden_door);
            }
            holder.setOnClickListener(R.id.tv_custom, view -> onSeriesTabChange(position, item, 0, holder, R.id.tv_custom));  //选中“定制”
            holder.setOnClickListener(R.id.tv_cupboard, view -> onSeriesTabChange(position, item, 1, holder, R.id.tv_cupboard)); //选中“橱柜”
            holder.setOnClickListener(R.id.tv_wooden_door, view -> onSeriesTabChange(position, item, 2, holder, R.id.tv_wooden_door)); //选中“木门”
            holder.setText(R.id.tv_performance, item.performance);  //TOP5业绩
            PieChartView pieChart = holder.obtainView(R.id.pieChart);
            pieChart.setPieChartDataList(item.pieChartData);
            setMultiPerformance(holder, item, true);
        }

        private void onSeriesTabChange(int position, SeriesPerformanceItem item, int tabIndex, RecyclerHolder holder, int selectId) {
            if (mSeriesTab != tabIndex) {
                mSeriesTab = tabIndex;
                setSelected(holder, selectId);
                item.setTabIndex(mSeriesTab);
                mDatas.set(position, item);  //系列业绩index为5
                notifyDataSetChanged();
            }
        }

        /*花色、系列通用*/
        private void setMultiPerformance(RecyclerHolder holder, MultiPerformanceItem item, boolean showIcon) {
            int visibility = showIcon ? View.VISIBLE : View.INVISIBLE;
            //first
            if (item.theFirst == null) {
                holder.setVisibility(R.id.ll_the_first, View.GONE);
            } else {
                holder.setVisibility(R.id.ll_the_first, View.VISIBLE);
                holder.setVisibility(R.id.iv_the_first, visibility);
                holder.setText(R.id.tv_the_first, item.theFirst);
                holder.setDrawableLeft(R.id.tv_the_first, getColorDrawable(0));
            }
            //second
            if (item.theSecond == null) {
                holder.setVisibility(R.id.ll_the_second, View.GONE);
            } else {
                holder.setVisibility(R.id.ll_the_second, View.VISIBLE);
                holder.setVisibility(R.id.iv_the_second, visibility);
                holder.setText(R.id.tv_the_second, item.theSecond);
                holder.setDrawableLeft(R.id.tv_the_second, getColorDrawable(1));
            }
            //third
            if (item.theThird == null) {
                holder.setVisibility(R.id.ll_the_third, View.GONE);
            } else {
                holder.setVisibility(R.id.ll_the_third, View.VISIBLE);
                holder.setVisibility(R.id.iv_third, visibility);
                holder.setText(R.id.tv_third, item.theThird);
                holder.setDrawableLeft(R.id.tv_third, getColorDrawable(2));
            }
            //fourth
            if (item.theFourth == null) {
                holder.setVisibility(R.id.ll_the_four, View.GONE);
            } else {
                holder.setVisibility(R.id.ll_the_four, View.VISIBLE);
                holder.setVisibility(R.id.tv_ranking_four, visibility);
                holder.setText(R.id.tv_ranking_four, "4");
                holder.setText(R.id.tv_fourth, item.theFourth);
                holder.setDrawableLeft(R.id.tv_fourth, getColorDrawable(3));
            }
            //fifth
            if (item.theFifth == null) {
                holder.setVisibility(R.id.ll_the_fifth, View.GONE);
            } else {
                holder.setVisibility(R.id.ll_the_fifth, View.VISIBLE);
                holder.setVisibility(R.id.tv_ranking_five, visibility);
                holder.setText(R.id.tv_ranking_five, "5");
                holder.setText(R.id.tv_fifth, item.theFifth);
                holder.setDrawableLeft(R.id.tv_fifth, getColorDrawable(4));
            }
            //other
            if (item.theOther == null) {
                holder.setVisibility(R.id.tv_other, View.GONE);
            } else {
                holder.setVisibility(R.id.tv_other, View.VISIBLE);
                holder.setText(R.id.tv_other, item.theOther);
                holder.setDrawableLeft(R.id.tv_other, getColorDrawable(5));
            }
        }

        /*客户来源*/
        private void setCustomerSource(final RecyclerHolder holder, final CustomerSourceItem item, final int position) {
            setSelected(holder, mSourceTab == 0 ? R.id.tv_tab_one : R.id.tv_tab_two);
            holder.setOnClickListener(R.id.tv_tab_one, view -> onCustomerSourceTabChange(position, item, 0, holder, R.id.tv_tab_one));
            holder.setOnClickListener(R.id.tv_tab_two, view -> onCustomerSourceTabChange(position, item, 1, holder, R.id.tv_tab_two));
            PieChartView pieChart = holder.obtainView(R.id.pieChart);
            pieChart.setPieChartDataList(item.pieChartData);
            holder.setText(R.id.tv_performance, item.performance); //新建客户数
            holder.setText(R.id.tv_yoy_growth, item.yearOnYear);   //同比增长
            if (!TextUtils.isEmpty(item.ringGrowth)) {
                holder.setVisibility(R.id.tv_ring_growth, View.VISIBLE);
                holder.setText(R.id.tv_ring_growth, item.ringGrowth);   //环比增长
            } else {
                holder.setVisibility(R.id.tv_ring_growth, View.GONE);
            }
            setMultiPerformance(holder, item, false);
        }

        private void onCustomerSourceTabChange(int position, CustomerSourceItem item, int tabIndex, RecyclerHolder holder, int selectId) {
            if (mSourceTab != tabIndex) {
                setSelected(holder, selectId);
                mSourceTab = tabIndex;
                item.setTabIndex(mSourceTab);
                mDatas.set(position, item);
                notifyDataSetChanged();
            }
        }

        /*客户分析*/
        private void setCustomerAnalysis(RecyclerHolder holder, CustomerAnalysisItem item) {
            holder.setText(R.id.tv_customer_price, item.customerPrice); //客单价
            holder.setDrawableRight(R.id.tv_customer_price, item.customerPriceDrawableId);
            holder.setText(R.id.tv_period, item.period); //平均成交周期
            holder.setDrawableRight(R.id.tv_period, item.periodDrawableId);
            holder.setText(R.id.tv_sign_order, item.signOrder);
            holder.setText(R.id.tv_order_install, item.orderInstall);
        }

        /*客户满意度*/
        private void setCustomerSatisfaction(RecyclerHolder holder, CustomerSatisfactionItem item) {
            holder.setText(R.id.tv_satisfaction, item.satisfaction);
            holder.setText(R.id.tv_service_satisfaction, item.service);
            holder.setDrawableRight(R.id.tv_service_satisfaction, item.drawableId);
            holder.setText(R.id.tv_delivery_satisfaction, item.delivery);
            holder.setDrawableRight(R.id.tv_delivery_satisfaction, item.drawableId);
            holder.setText(R.id.tv_design_satisfaction, item.design);
            holder.setDrawableRight(R.id.tv_design_satisfaction, item.drawableId);
            holder.setText(R.id.tv_install_satisfaction, item.install);
            holder.setDrawableRight(R.id.tv_install_satisfaction, item.drawableId);
        }

        /*门店效益*/
        private void setShopBenefit(RecyclerHolder holder, ShopBenefitItem item) {
            holder.setText(R.id.tv_effect, item.effect);
            holder.setDrawableRight(R.id.tv_effect, item.effectDrawableId);
            holder.setText(R.id.tv_total_human_effect, item.totalEffect);
            holder.setDrawableRight(R.id.tv_total_human_effect, item.userEffectDrawableId);
            holder.setText(R.id.tv_sales_effectiveness, item.salesEffect);
            holder.setDrawableRight(R.id.tv_sales_effectiveness, item.salesEffectDrawableId);
        }

        /*盈利状况*/
//        private void setProfitability(RecyclerHolder holder, ProfitabilityItem item) {
//            holder.setText(R.id.tv_payback, item.payback);
//            holder.setDrawableRight(R.id.tv_payback, item.getDrawable(item.isPaybackRise));
//            holder.setText(R.id.tv_gross_profit, item.grossProfit);
//            holder.setText(R.id.tv_gross_profit_rate, item.grossProfitRate);
//            holder.setText(R.id.tv_growth_left, item.growthLeft);
//            holder.setText(R.id.tv_operating_cost, item.operatingCost);
//            holder.setText(R.id.tv_net_income, item.netIncome);
//            holder.setDrawableRight(R.id.tv_net_income, item.getDrawable(item.isNetIncomeRise));
//            holder.setText(R.id.tv_net_income_rate, item.netIncomeRate);
//            holder.setDrawableRight(R.id.tv_net_income_rate, item.getDrawable(item.isNetIncomeRateRise));
//            holder.setText(R.id.tv_growth_right, item.growthRight);
//            holder.setDrawableRight(R.id.tv_growth_right, item.getDrawable(item.isGrowthRightRise));
//        }

        /*经销商排名-排名数据*/
        private void setDealerRankingOne(RecyclerHolder holder, DealerRankingOneItem item) {
            holder.setText(R.id.tv_performance_ranking, item.performance); //业绩排名
            holder.setText(R.id.tv_entry_turnover_rate_rinking, item.turnoverRate); //进店成交率排名
            holder.setText(R.id.tv_effect_ranking, item.effect); //坪效排名
            holder.setText(R.id.tv_customer_price_ranking, item.customerPrice); //客单价排名
            holder.setText(R.id.tv_scale_turnover_rate_rinking, item.scaleRate); //量尺成交率排名
            holder.setText(R.id.tv_human_effect_ranking, item.humanEffect); //人效排名
        }

        /*经销商排名-折线图*/
        private void setDealerRankingTwo(RecyclerHolder holder, DealerRankingSecondItem item) {
            LineChartView lineChartView = holder.obtainView(R.id.lineChartView);
            lineChartView.setData(item.dataList);
        }

        /*经销商排名-当前位置*/
        private void setDealerRankingThird(RecyclerHolder holder, DealerRankingThirdItem item) {
            holder.setImageResource(R.id.iv_danger, item.dangerDrawableId);
            holder.setTextColor(R.id.tv_danger, item.dangerTextColor);
            holder.setImageResource(R.id.iv_strive, item.striveDrawableId);
            holder.setTextColor(R.id.tv_strive, item.striveTextColor);
            holder.setImageResource(R.id.iv_potential, item.potentialDrawableId);
            holder.setTextColor(R.id.tv_potential, item.potentialTextColor);
            holder.setImageResource(R.id.iv_excellent, item.excellentDrawableId);
            holder.setTextColor(R.id.tv_excellent, item.excellentTextColor);
            holder.setImageResource(R.id.iv_outstanding, item.outstandingDrawableId);
            holder.setTextColor(R.id.tv_outstanding, item.outstandingTextColor);
            if (item.currentPosition == 1) {
                setVisibility(holder.obtainView(R.id.iv_location_one), holder.obtainView(R.id.tv_location_one), item.leftMargin);
            } else if (item.currentPosition == 2) {
                setVisibility(holder.obtainView(R.id.iv_location_two), holder.obtainView(R.id.tv_location_two), item.leftMargin);
            } else if (item.currentPosition == 3) {
                setVisibility(holder.obtainView(R.id.iv_location_three), holder.obtainView(R.id.tv_location_three), item.leftMargin);
            } else if (item.currentPosition == 4) {
                setVisibility(holder.obtainView(R.id.iv_location_four), holder.obtainView(R.id.tv_location_four), item.leftMargin);
            } else if (item.currentPosition == 5) {
                setVisibility(holder.obtainView(R.id.iv_location_five), holder.obtainView(R.id.tv_location_five), item.leftMargin);
            } else {
                setInvisibility(holder.obtainView(R.id.iv_location_one), holder.obtainView(R.id.tv_location_one),
                        holder.obtainView(R.id.iv_location_two), holder.obtainView(R.id.tv_location_two),
                        holder.obtainView(R.id.iv_location_three), holder.obtainView(R.id.tv_location_three),
                        holder.obtainView(R.id.iv_location_four), holder.obtainView(R.id.tv_location_four),
                        holder.obtainView(R.id.iv_location_five), holder.obtainView(R.id.tv_location_five));
            }
        }

        private void setVisibility(ImageView ivPosition, TextView tvPosition, int leftMargin) {
            ivPosition.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ivPosition.getLayoutParams();
            lp.leftMargin = leftMargin;
            ivPosition.setLayoutParams(lp);
            tvPosition.setVisibility(View.VISIBLE);
        }

        private void setInvisibility(View... views) {
            for (View view : views) {
                view.setVisibility(View.INVISIBLE);
            }
        }

        private void setDealerRankingFour(RecyclerHolder holder, DealerRankingFourItem item) {
            holder.setText(R.id.tv_second, item.top2Name);
            holder.setText(R.id.tv_first, item.top1Name);
            holder.setText(R.id.tv_third, item.top3Name);
        }

        /*点击后选中处理*/
        private void setSelected(RecyclerHolder holder, int selectViewId) {
            holder.obtainView(selectViewId).setSelected(true);
            LinearLayout llTab = holder.obtainView(R.id.ll_tab);
            for (int i = 0; i < llTab.getChildCount(); i++) {
                View view = llTab.getChildAt(i);
                if (view instanceof TextView && view.getId() != selectViewId) {
                    view.setSelected(false);
                }
            }
        }
    }

    private Drawable getColorDrawable(int position) {
        if (position < 0 || position >= mColorArray.length) return null;
        GradientDrawable drawable = (GradientDrawable) ContextCompat.getDrawable(mContext, R.drawable.bg_corners2dp_multiple);
        if (drawable != null) {
            drawable.setColor(mColorArray[position]);
        }
        return drawable;
    }

    interface Callback {
        void onRequest(String shopCode, String month);
    }
}
